<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {
	public function index(){
		if($this->session->userdata('user_id') == '' || $this->session->userdata('user_name') == ''){
			$this->load->helper(array('url'));
			redirect('/user/login', 'location', 301);
		}
		else{
		//Check Domain And Hosting is Closed
			$sql = "SELECT Count(domain_id) as Subdomain FROM tdx_domain WHERE status = 0";
			$querysql = $this->db->query($sql);
			foreach ($querysql->result_array() as $row){	
				if (intval($row['Subdomain']) > 0){				
					$data['domainoff'] = intval($row['Subdomain']);
				}
			}			
			
			$sql = "SELECT Count(domain_id) as Subdomain FROM tdx_domain WHERE closed = 1";
			$querysql = $this->db->query($sql);
			foreach ($querysql->result_array() as $row){
				if (intval($row['Subdomain']) > 0){					
					$data['domainclosed'] = intval($row['Subdomain']);
				}
			}	
			
			$sql = "SELECT Count(domain_id) as Subdomain FROM tdx_domain WHERE DATE_SUB(end_date,INTERVAL 20 DAY) <= '" .date('Y-m-d')."'";
			$querysql = $this->db->query($sql);
			foreach ($querysql->result_array() as $row){					
				if (intval($row['Subdomain']) > 0){
					$data['domainwilloff'] = intval($row['Subdomain']);
				}
			}			
			
			$sql = "SELECT Count(host_id) as Subhost FROM tdx_host WHERE status = 0";
			$querysql = $this->db->query($sql);
			foreach ($querysql->result_array() as $row){	
				if (intval($row['Subhost']) > 0){				
					$data['hostoff'] = intval($row['Subhost']);
				}
			}			
			
			$sql = "SELECT Count(host_id) as Subhost FROM tdx_host WHERE closed = 1";
			$querysql = $this->db->query($sql);
			foreach ($querysql->result_array() as $row){
				if (intval($row['Subhost']) > 0){					
					$data['hostclosed'] = intval($row['Subhost']);
				}
			}	
			
			$sql = "SELECT Count(host_id) as Subhost FROM tdx_host WHERE DATE_SUB(end_date,INTERVAL 20 DAY) <= '" .date('Y-m-d')."'";
			$querysql = $this->db->query($sql);
			foreach ($querysql->result_array() as $row){					
				if (intval($row['Subhost']) > 0){
					$data['hostwilloff'] = intval($row['Subhost']);
				}
			}			
			//===============================
			
			#$this->session->userdata('user_id').'|'.$this->session->userdata('user_name');
			$this->layout->setLayout('default');
			$this->layout->setTitle('Administrator | Home');
			if(isset($_GET['date'])){
				$arrDate = explode("-", $_GET['date']);
				$month = $arrDate[0].'-'.$arrDate[1];
			}else{
				$year = isset($_GET['year']) ? $_GET['year'] : date('Y');
				$month = isset($_GET['month']) ? $year . '-'.str_pad(quotes_to_entities($_GET['month']), 2, "0", STR_PAD_LEFT) : date('Y-m');
			}
			$arrDate = explode("-", $month);
			$data['month'] = $arrDate[1].'-'.$arrDate[0];
			//Tinh so ngay trong thang
			$day = cal_days_in_month(CAL_GREGORIAN, $arrDate[1],$arrDate[0]);
			// Use Procedure in Phpmyadmin ======================================
			$con = mysql_connect($this->db->hostname, $this->db->username, $this->db->password, false,65536);
			mysql_select_db($this->db->database, $con);
			$total = 0;
			$totalcustomer = 0;
			$sql = "call spSubTotalMonth('".$month."-01 00:00:00','".$month."-".$day." 23:59:59');";
			$queryUser1 = mysql_query($sql);
			
			if ($queryUser1 === FALSE) {
				die(mysql_error());
			}
			$arrData = array();
			
			while($row = mysql_fetch_array($queryUser1))
			{				
				$data['items2'][] = $row;
				$arrData[$row['earn_date']] += intval($row['earm_day']);
				$total += intval($row['earm_day']);
				$totalcustomer += intval($row['earm_day_site']);
			}
			
			mysql_close($con);
			// ==================================================================			
			
			if(isset($_GET['date'])){
				$today = $_GET['date'];				
				$strDate = 'Ngày '.$_GET['date'];				
				// Use Procedure in Phpmyadmin ======================================
				$con = mysql_connect($this->db->hostname, $this->db->username, $this->db->password, false,65536);
				mysql_select_db($this->db->database, $con);
				$sql1 = "call spSubTotalMonth('".$today." 00:00:00','".$today." 23:59:59');";
				$queryUser1 = mysql_query($sql1);
				if ($queryUser1 === FALSE) {
					die(mysql_error());
					$data['earn_today'] = 0;
				}
				
				while($row = mysql_fetch_array($queryUser1))
				{				
					$data['earn_today'] += intval($row['earm_day']);
				}
				
				mysql_close($con);
				// ==================================================================
			}
			else{
				if (!isset($_GET['month'])){
					$strDate = 'Hôm nay';
					$data['earn_today'] = intval($arrData[date('d-m-Y')]);
				}
			}
			$data['str_date'] = $strDate;
			$data['earn_this_month_customer'] = $totalcustomer;
			$data['earn_this_month'] = $total;			
			$data['month_data'] = $arrData;
			//$data['month_now'] = $sql;
			
			
			$this->layout->view(THEME_DIR.'/home_index', $data);
		}
	}
}